package com.solution.pc.my.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.company.entity.CompanyInvoiceEntity;
import com.solution.admin.company.service.CompanyInvoiceService;
import com.solution.admin.customer.entity.CompanyEntity;
import com.solution.admin.customer.service.CompanyService;
import com.solution.admin.point.constants.PointConstants.IsYesNo;
import com.solution.common.constants.PublicStatus;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.controller.BaseController;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 公司开票信息
 * @author xkl
 * @date 2018年1月26日 下午3:37:10
 */
@RequestMapping("/pc/my/companyInvoice")
@Controller("MyCompanyInvoiceController")
public class MyCompanyInvoiceController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(MyCompanyInvoiceController.class);

	@Autowired
	private CompanyInvoiceService companyInvoiceService;

	@Autowired
	private CompanyService companyService;
	
	/**
	 * 获取公司开票信息分页列表
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2018年1月26日 下午3:42:32
	 */
	@RequestMapping("")
	public String listCompanyInvoicePage(HttpServletRequest request, Model model) {
		Long companyId = getCompanyId();
		PageParam pageParam = getPageParam(request, 5);
		try {
			if (null != companyId) {
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("companyId", companyId);
				paramMap.put("status", PublicStatus.STATUS.ENABLED.getId());
				paramMap.put("isDelete", PublicStatus.IS_DELETE.NORMAL.getId());
				PageBean pageBean = companyInvoiceService.listPage(pageParam, paramMap);
				CompanyEntity company = companyService.getCompanyById(getCompanyId());
				model.addAttribute("company", company);
				model.addAttribute("pageBean", pageBean);
			}
			model.addAttribute("page", "invoice");
		} catch (Exception e) {
			handleException("获取公司开票信息异常", logger, e);
		}
		return "pc/my/invoice/my_invoice";
	}
	
	/**
	 * 获取发票列表
	 * @param request
	 * @param model
	 * @return
	 * @author: solin
	 * @date: 2018年1月28日 下午3:02:58
	 */
	@RequestMapping("listCompanyInvoice")
	@ResponseBody
	public List<CompanyInvoiceEntity> listCompanyInvoice(HttpServletRequest request, Model model) {
		List<CompanyInvoiceEntity> companyInvoiceList = new ArrayList<CompanyInvoiceEntity>();
		Long companyId = getCompanyId();
		try {
			if (null != companyId) {
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("companyId", companyId);
				paramMap.put("status", PublicStatus.STATUS.ENABLED.getId());
				paramMap.put("isDelete", PublicStatus.IS_DELETE.NORMAL.getId());
				companyInvoiceList = companyInvoiceService.listBy(paramMap);
			}
			model.addAttribute("page", "invoice");
		} catch (Exception e) {
			handleException("获取公司开票信息异常", logger, e);
		}
		return companyInvoiceList;
	}

	/**
	 * 根据id获取发票信息
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2018年1月26日 下午5:04:16
	 */
	@RequestMapping("getCompanyInvoiceById")
	@ResponseBody
	public Map<String, Object> getCompanyInvoiceById(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String invoiceIdStr = request.getParameter("invoiceId");
		if (null == invoiceIdStr || "".equals(invoiceIdStr)) {
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "无开票信息");
			return resultMap;
		}
		Long invoiceId = Long.parseLong(invoiceIdStr);
		try {
			CompanyInvoiceEntity companyInvoice = companyInvoiceService.getById(invoiceId);
			CompanyEntity companyInfo = getCompany();
			resultMap.put("companyInvoice", companyInvoice);
			resultMap.put("companyInfo", companyInfo);
			resultMap.put(PublicStatus.RESULT_CODE, "1");
			resultMap.put(PublicStatus.RESULT_MSG, "操作成功");
		} catch (Exception e) {
			handleException("获取公司发票信息异常", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
		}
		return resultMap;
	}

	/**
	 * 设置默认发票信息
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2018年1月27日 下午4:57:12
	 */
	@RequestMapping("doSetDefault")
	@ResponseBody
	public Map<String,Object> doSetDefault(HttpServletRequest request) {
		Long invoiceId = Long.parseLong(request.getParameter("invoiceId"));
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (StringUtil.isNotNull(invoiceId)) {
				CompanyInvoiceEntity invoice = new CompanyInvoiceEntity();
				invoice.setId(invoiceId);
				invoice.setIsDefault(Integer.parseInt(IsYesNo.YES));
				invoice.setModifier(getCustomer().getUserName());
				invoice.setCompanyId(getCompanyId());
				int result = companyInvoiceService.updateDefaultInvoice(invoice);
				if (result > 0) {
					resultMap.put(PublicStatus.RESULT_CODE, SysCode.SUCCESS);
					resultMap.put(PublicStatus.RESULT_MSG, "设置成功~");
				} else {
					resultMap.put(PublicStatus.RESULT_CODE, SysCode.ERROR);
					resultMap.put(PublicStatus.RESULT_MSG, "设置失败，请重试！");
				}
			} else {
				resultMap.put(PublicStatus.RESULT_CODE, SysCode.ERROR);
				resultMap.put(PublicStatus.RESULT_MSG, "设置失败，请重试！");
			}
		} catch (Exception e) {
			handleException("获取公司开票信息异常", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, SysCode.ERROR);
			resultMap.put(PublicStatus.RESULT_MSG, "设置失败，请重试！");
		}
		return resultMap;
	}
	
	/**
	 * 保存或修改发票信息
	 * @param request
	 * @param model
	 * @param invoice
	 * @return
	 * @author xkl
	 * @date 2018年1月27日 下午6:10:10
	 */
	@RequestMapping("/saveInvoice")
	public String saveInvoice(HttpServletRequest request, Model model, CompanyInvoiceEntity invoice) {
		try {
			if (null != invoice) {
				if (invoice.getIsDefault() == null) {
					invoice.setIsDefault(0);
				}
				invoice.setCompanyId(getCompanyId());
				if (null != invoice.getId()) {
					invoice.setModifier(getUserName());
					companyInvoiceService.update(invoice);
				} else {
					invoice.setCreater(getUserName());
					CompanyInvoiceEntity oldInvoice = companyInvoiceService.getByCompanyName(getCompanyId(), invoice.getCompanyName());
					if (null != oldInvoice) {
						companyInvoiceService.deleteById(oldInvoice.getId());
					}
					companyInvoiceService.create(invoice);
				}
			}
			request.setAttribute("tabIndex", 3);
		} catch (Exception e) {
			handleException("保存公司开票信息异常", logger, e);
		}
		return "redirect:/pc/my/companyInvoice";
	}

	/**
	 * 异步保存开票信息
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2018年3月6日 下午4:50:07
	 */
	@RequestMapping("/saveInvoiceAjax")
	@ResponseBody
	public Map<String, Object> saveInvoiceAjax(HttpServletRequest request){
		String companyName = request.getParameter("companyName");
		String taxNo = request.getParameter("taxNo");
		String registeAddress = request.getParameter("registeAddress");
		String registePhone = request.getParameter("registePhone");
		String openBankName = request.getParameter("openBankName");
		String bankAccount = request.getParameter("bankAccount");
		CompanyInvoiceEntity invoice = new CompanyInvoiceEntity();
		invoice.setCompanyName(companyName);
		invoice.setTaxNo(taxNo);
		invoice.setRegisteAddress(registeAddress);
		invoice.setRegistePhone(registePhone);
		invoice.setOpenBankName(openBankName);
		invoice.setBankAccount(bankAccount);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (null != invoice) {
				if (invoice.getIsDefault() == null) {
					invoice.setIsDefault(0);
				}
				invoice.setCompanyId(getCompanyId());
				invoice.setCreater(getUserName());
				CompanyInvoiceEntity oldInvoice = companyInvoiceService.getByCompanyName(getCompanyId(), invoice.getCompanyName());
				if (null != oldInvoice) {
					companyInvoiceService.deleteById(oldInvoice.getId());
				}
				companyInvoiceService.create(invoice);
			}
			resultMap.put(PublicStatus.RESULT_CODE, SysCode.SUCCESS);
			resultMap.put(PublicStatus.RESULT_MSG, "保存开票信息成功~");
		} catch (Exception e) {
			handleException("保存公司开票信息异常", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, SysCode.ERROR);
			resultMap.put(PublicStatus.RESULT_MSG, "保存开票信息失败，请重试！");
		}
		return resultMap;
	}
	
	/**
	 * 删除发票信息
	 * @param request
	 * @param invoiceId
	 * @return
	 * @author xkl
	 * @date 2018年1月27日 下午5:02:01
	 */
	@RequestMapping("/doDelCompanyInvoice")
	@ResponseBody
	public Map<String,Object> doDelCompanyInvoice(HttpServletRequest request, String invoiceId) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			int rows = companyInvoiceService.deleteById(Long.parseLong(invoiceId));
			if (rows > 0) {
				resultMap.put(PublicStatus.RESULT_CODE, SysCode.SUCCESS);
				resultMap.put(PublicStatus.RESULT_MSG, "删除成功~");
			} else {
				resultMap.put(PublicStatus.RESULT_CODE, SysCode.ERROR);
				resultMap.put(PublicStatus.RESULT_MSG, "删除失败，请重试！");
			}
		} catch (Exception e) {
			handleException("删除公司开票信息异常", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, SysCode.ERROR);
			resultMap.put(PublicStatus.RESULT_MSG, "删除失败，请重试！");
		}
		return resultMap;
	}
}
